package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.TimeZoneUtil;
import util.enviaEmail;
import bean.comissaoBean;
import bean.designerBean;
import bean.secretarioBean;
import bean.trocaOsBean;
import conexao.Conexao;
import dao.OSDao;
import dao.comissaoDao;
import dao.designerDao;
import dao.secretarioDao;

public class trocarOsDesignerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public trocarOsDesignerServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		PrintWriter out = response.getWriter();
		
		Connection conn = null;
		try {
			conn = new Conexao().getConexao();
		} catch (SQLException e) {
			new enviaEmail().enviar(e);
			out.print("falhaConexao");
			return;
		}

		try {

			String motivo = "";
			if (!request.getParameter("texto").equals("")) {

				Integer idOs = Integer.parseInt(request.getParameter("idOs"));

				Integer idDesigner = Integer.parseInt(request.getParameter("idDesigner"));

				designerBean des = new designerBean();
				des.setIdos(idOs);
				designerBean designerAnterior = new designerDao().getdesigner(conn, des);

				secretarioBean sec = new secretarioBean();
				sec.setIdos(idOs);
				secretarioBean secretarioAnterior = new secretarioDao().getSecretario(conn, sec);

				motivo = request.getParameter("texto");

				trocaOsBean bean = new trocaOsBean();
				bean.setIdos(idOs);
				bean.setMotivo(motivo);
				bean.setDataTroca(new TimeZoneUtil().getHoraLocal());
				bean.setSecretarioAnterior(secretarioAnterior.getIdsecretario());
				bean.setSecretarioAtual((Integer) request.getSession().getAttribute("id"));
				bean.setDesignerAnterior(designerAnterior.getIddesigner());
				bean.setDesignerAtual(idDesigner);
				
				comissaoBean comissao = new comissaoBean();
				comissao.setIdFuncionario(idDesigner);
				comissao.setIdOS(idOs);
				
				new comissaoDao().alterarDesigner(conn, comissao);

				new OSDao().trocarDesigner(conn, bean, (Integer) request
						.getSession().getAttribute("id"), Integer
						.parseInt(request.getParameter("idDesigner")));
				conn.commit();
			} else {
				out.print("Você tem que informar um motivo!");
			}
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (Exception ex) {
				new enviaEmail().enviar(ex);
				ex.printStackTrace();
			}
			new enviaEmail().enviar(e);
			throw new RuntimeException(e);
		} finally {
			try {
				conn.close();
			} catch (Exception ex) {
				new enviaEmail().enviar(ex);
			}
		}
	}

}
